package com.xiexie.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @Description
 * @Date 2022-04-07 14:46
 * @Author xie
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WebClient webClient;

    @Autowired
    private ReactorLoadBalancerExchangeFilterFunction lbFunction;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功");

        //String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);

        String msg = restTemplate.getForObject("http://stock-server/stock/reduct", String.class);

        // 基于WebClient
        Mono<String> bodyToMono = webClient.get().uri("http://stock-server/stock/reduct").retrieve().bodyToMono(String.class);

        // 基于WebClient+webFlux
        Mono<String> stringMono = WebClient.builder()
                .filter(lbFunction)
                .build()
                .get()
                .uri("http://stock-server/stock/reduct")
                .retrieve()
                .bodyToMono(String.class);


        return "Hello order add " + msg;
    }


}
